package m202405.d14;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/14 15:08
 */
public class Q33 {

    public static void main(String[] args) {
        Q33 q33 = new Q33();
        q33.search(new int[]{1,3}, 1);
    }

    public int search(int[] nums, int target) {
        return find(nums, 0, nums.length, target);
    }

    private int find(int[] nums, int start, int end, int target) {
        if (start + 1 == end) {
            return nums[start] == target ? start : -1;
        }
        int mid = (start + end) / 2;
        if (target == nums[mid]) {
            return mid;
        }
        if (nums[start] <= nums[mid - 1]) {
            if (target <= nums[mid - 1] && target >= nums[start]) {
                return find(nums, start, mid, target);
            } else {
                return find(nums, mid, end, target);
            }
        }
        if (nums[mid] <= nums[end - 1]) {
            if (target > nums[mid] && target <= nums[end - 1]) {
                return find(nums, mid, end, target);
            } else {
                return find(nums, start, mid, target);
            }
        }
        return -1;
    }

}
